﻿using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Xml.Linq;
using LinqToLfsWorld.Extensions;

namespace LinqToLfsWorld.Queries
{
	/// <summary>
	/// Queries the hotlap log on LfsWorld
	/// </summary>
	public class HotlapLogQuery : LfsWorldQuery<HotlapLogEntry>
	{
		/// <summary>
		/// Initializes a new instance of the <see cref="HotlapLogQuery"/> class.
		/// </summary>
		/// <param name="context">The context.</param>
		public HotlapLogQuery(LfsWorldContext context)
			: base(context)
		{
		}

		/// <summary>
		/// Gets the URL used to query LfsWorld.
		/// </summary>
		protected override string GetUrl()
		{
			string url = LfsHelper.GetPubstatUrlBase(PubstatResponseType.Xml, this, "hl_log");

			if (ExpressionValues.ContainsKey("LogFilter"))
				url += "&log_filter=" + ExpressionValues["LogFilter"].Value.ToString();

			if (ExpressionValues.ContainsKey("Steering"))
				url += "&control=" + (string)ExpressionValues["Steering"].Value;

			if (ExpressionValues.ContainsKey("Lines"))
			{
				int lines = (int)ExpressionValues["Lines"].Value;

				if (lines > 0 && lines <= 150)
					url += "&lines=" + lines.ToString();
			}

			if (ExpressionValues.ContainsKey("StartTime"))
				url += "&starttime=" + ExpressionValues["StartTime"].Value;

			return url;
		}

		/// <summary>
		/// Gets the entities given LfsWorld response XML
		/// </summary>
		/// <param name="responseData">The response data.</param>
		/// <returns>A collection of Host entities</returns>
		protected override IEnumerable<HotlapLogEntry> ProcessResponse(string responseData)
		{
			XDocument doc = XDocument.Parse(responseData);

			foreach (XElement node in doc.Descendants("entry"))
			{

				HotlapLogEntry entry = new HotlapLogEntry();

				entry.RacerName = node.Element("racername").Value.StripLfsColours();

				int ts = Convert.ToInt32(node.Element("timestamp").Value);
				entry.Timestamp = DateTimeExtensions.FromUnixTimestamp(ts);
				entry.Country = node.Element("country").Value;
				entry.TrackIdent = node.Element("track").Value;
				entry.TrackName = Utility.CircuitNameFactory.GetNameFromIdent(entry.TrackIdent);
				entry.Car = node.Element("car").Value;
				entry.Split1 = TimeSpan.FromMilliseconds(Convert.ToInt32(node.Element("split1").Value));
				entry.Split2 = TimeSpan.FromMilliseconds(Convert.ToInt32(node.Element("split2").Value));
				entry.Split3 = TimeSpan.FromMilliseconds(Convert.ToInt32(node.Element("split3").Value));
				entry.Split4 = TimeSpan.FromMilliseconds(Convert.ToInt32(node.Element("split4").Value));
				entry.Position = Convert.ToInt32(node.Element("pos").Value);
				entry.Flags = (HotlapFlags)Convert.ToInt32(node.Element("flags_hlaps").Value);
				entry.Id = Convert.ToInt32(node.Element("id_hl").Value);
				entry.LogFilter = (HotlapLogFilter)ExpressionValues["LogFilter"].Value;

				yield return entry;

			}

			yield break;
		}
	}
}
